--[[
	Copyright (C) Udorn (Blackhand)
--]]

local MAJOR, MINOR = "YakaGui", 1
local YakaGui = LibStub:NewLibrary(MAJOR, MINOR)

if (not YakaGui) then
	return 
end

YakaGui.views = {}
YakaGui.viewTypes = {}
YakaGui.layouts = {}
YakaGui.nextId = 1

local log = vendor.Debug:new("YakaGui")

local s2 = sqrt(2)
local cos, sin, rad = math.cos, math.sin, math.rad

local function _CalculateCorner(angle)
	local r = rad(angle)
	return 0.5 + cos(r) / s2, 0.5 + sin(r) / s2
end


function YakaGui:RegisterViewType(view)
	self.viewTypes[view.GetViewInfo().viewType] = view
end

function YakaGui:RegisterLayoutType(layout)
	self.layouts[layout.GetLayoutInfo().layoutType] = layout
end

function YakaGui:GetViewInfo(viewType)
	local view = self.viewTypes[viewType]
	if (view) then
		return view.GetViewInfo()
	end
	return nil
end

function YakaGui:GetLayoutInfo(layoutType)
	local layout = self.layouts[layoutType]
	if (layout) then
		return layout.GetLayoutInfo()
	end
	return nil
end

function YakaGui:CreateView(cfg)
    log:Debug("CreateView viewType [%s]", cfg.viewType)
	if (not cfg) then
		error("can't create view without cfg definition")
	end
	if (not cfg.viewType) then
		error("can't create view without viewType definition")
	end
	local view = self.viewTypes[cfg.viewType]
	if (not view) then
		error("can't find view of type ["..cfg.viewType.."]")
	end
	return view:new(cfg)
end

function YakaGui:CreateLayout(cfg)
	if (not cfg) then
		error("can't create layout without cfg definition")
	end
	if (not cfg.layoutType) then
		error("can't create layout without layoutType definition")
	end
	local layout = self.layouts[cfg.layoutType]
	if (not layout) then
		error("can't find layout of type ["..cfg.layoutType.."]")
	end
	return layout:new(cfg)
end

--[[
	Rotates the given texture by the specified angle.
	Taken from http://www.wowwiki.com/SetTexCoord_Transformations
--]]
function YakaGui:RotateTexture(texture, angle)
	local LRx, LRy = _CalculateCorner(angle + 45)
	local LLx, LLy = _CalculateCorner(angle + 135)
	local ULx, ULy = _CalculateCorner(angle + 225)
	local URx, URy = _CalculateCorner(angle - 45)
	
	texture:SetTexCoord(ULx, ULy, LLx, LLy, URx, URy, LRx, LRy)
end

function YakaGui:CreateId()
	local rtn = self.nextId
	self.nextId = self.nextId + 1
	return rtn
end

function YakaGui:RegisterView(view)
	if (view:GetName()) then
		self.views[view:GetName()] = view
	end
end

function YakaGui:UnregisterView(view)
	if (view:GetName()) then
		self.views[view:GetName()] = nil
	end
end

function YakaGui:GetView(name)
	return self.views[name]
end

function YakaGui:GetFontColorCode(...)
	local n = select("#", ...)
	local r, g, b, a
	if (n == 1) then
		local color = select(1, ...)
   		r = math.floor(color.r * 255.0)
   		g = math.floor(color.g * 255.0)
   		b = math.floor(color.b * 255.0)
   	else
   		r, g, b = ...
   		r = r * 255.0
   		g = g * 255.0
   		b = b * 255.0
	end
	return string.format("|cff%2x%2x%2x", r, g, b)
end